using BackData;
using Domain;
using Domain.DTOs.Quality;
using Domain.RBAC;
using Domain.工艺工序;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.Quality;

namespace Team.API.Applocation.Hanldler.Quality
{
    /// <summary>
    /// 获取质检报工列表处理器
    /// </summary>
    public class GetQualityReportListHandler : IRequestHandler<GetQualityReportListCommand, APIPaging<QualityReportDto>>
    {
        private readonly IRepository<Qualitymodel> quality;
        private readonly IRepository<Makestepmodel> makeRep;
        private readonly IRepository<Usermodel> userRep;

        public GetQualityReportListHandler(
            IRepository<Qualitymodel> quality,
            IRepository<Makestepmodel> makeRep,
            IRepository<Usermodel> userRep)
        {
            this.quality = quality;
            this.makeRep = makeRep;
            this.userRep = userRep;
        }

        public async Task<APIPaging<QualityReportDto>> Handle(GetQualityReportListCommand request, CancellationToken cancellationToken)
        {
            // 先查询所有数据到内存
            var qualityList = quality.GetAll().Where(x => x.IsDel == false).ToList();
            var makeList = makeRep.GetAll().Where(x => x.IsDel == false).ToList();
            List<Usermodel>? userList = userRep.GetAll().Where(x => x.IsDel == false).ToList();

            // 在内存中进行关联
            var query = from q in qualityList
                        join m in makeList on q.Processid equals m.Id into mGroup
                        from m in mGroup.DefaultIfEmpty()
                        join u in userList on q.Accountid equals u.Id into uGroup
                        from u in uGroup.DefaultIfEmpty()
                        select new QualityReportDto
                        {
                            Id = q.Id,
                            Processcode = m.Processcode,
                            Processname = m.Processname ,
                            Processdescription = m.Processdescription,
                            Kahuna = m.Kahuna,
                            Departmentname = m.Departmentname,
                            Reportingtime = q.Reportingtime,
                            Completenum = q.Completenum,
                            ProductionStaff = u.RealName,
                            ResponsibleDepartment = m.Departmentname,
                            ReportingStaff = u.RealName,
                            Quaworkcode = q.Quaworkcode,
                            Quaworkname = q.Quaworkname,
                            Qualifiednum = q.Qualifiednum,
                            Defectivenum = q.Defectivenum,
                            Status = q.Status
                        };

            // 应用筛选条件
            if (!string.IsNullOrEmpty(request.Processcode))
            {
                query = query.Where(x => x.Processcode.Contains(request.Processcode));
            }


            // 排序
            var sortedQuery = query.OrderByDescending(x => x.Reportingtime);

            // 获取总数
            int totalCount = sortedQuery.Count();

            // 分页
            int pageIndex = request.PageIndex;
            int pageSize = request.PageSize;
            var pageData = sortedQuery
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList();

            return new APIPaging<QualityReportDto>
            {
                Totalcount = totalCount,
                Code = StateCode.OK,
                Message = "获取数据成功",
                ListResult = pageData
            };
        }
    }
}
